package com.softer.wxzj.common.util;/**
 * @author Gong Zhiyang
 * @date 2019/5/7
 */

import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.common.flow.enums.FlowTypeEnum;
import com.softer.wxzj.entity.*;
import com.softer.wxzj.mapper.*;
import com.sun.org.apache.bcel.internal.generic.NEWARRAY;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.util.*;

/**
 * @author Gong Zhiyang
 * @date 2019/5/7
 */
@Component
@Slf4j
public class ModifyUtil<T> {
    @Autowired
    private BaseModifyDetailMapper modifyDetailMapper;
    @Autowired
    private BaseModifyDetailMapper baseModifyDetailMapper;
    @Autowired
    private BaseModifyReqMapper baseModifyReqMapper;
    @Autowired
    private FlowInstanceMapper flowInstanceMapper;
    @Autowired
    private BaseBuildModifyDetailMapper baseBuildModifyDetailMapper;
    @Autowired
    private CombineEntityUtil<T> combineEntityUtil;
    @Autowired
    private BaseHouseInfoMapper baseHouseInfoMapper;
    @Autowired
    private BaseBuildInfoMapper baseBuildInfoMapper;
    @Autowired
    private BaseSubRegionInfoMapper baseSubRegionInfoMapper;
    @Autowired
    private BaseRegionInfoMapper baseRegionInfoMapper;
    @Autowired
    private ZoneCodeMapper zoneCodeMapper;
    @Autowired
    private BaseCellInfoMapper baseCellInfoMapper;

    /**
     * 封装变更查询参数
     *
     * @param id 业务ID
     * @return map
     */
    public Map<String, Object> getMap(String id) {
        List<String> list = modifyDetailMapper.getColumnList(id);
        Map<String, Object> map = new HashMap<>(8);
        map.put("list", list);
        map.put("id", id);
        return map;
    }

    /**
     * List<Map>转map(house专用)
     *
     * @param
     * @return
     */
    public Map<String, Object> ListNewToMap(String id, String reqId) {
        List<Map<String, Object>> list = modifyDetailMapper.getDetailListByBusiness(id, reqId);
        Map<String, Object> resultMap = new HashMap<>(8);
        for (Map<String, Object> map : list) {
            String mapKey = String.valueOf(map.get("mapKey"));
            String mapValue = null;
            if (!"".equals(String.valueOf(map.get("mapValue")))) {
                mapValue = String.valueOf(map.get("mapValue"));
            }
            resultMap.put(mapKey, mapValue);
        }
        for (String key : resultMap.keySet()) {
            log.info(key + "-------" + resultMap.get(key));
        }
        return resultMap;
    }

    /**
     * List<Map>转map(house结构变更专用)
     *
     * @param
     * @return
     */
    public Map<String, Object> ListNewStrutureToMap(String id, String reqId) {
        List<Map<String, Object>> list = modifyDetailMapper.getDetailListByBusinessNextId(id, reqId);
        Map<String, Object> resultMap = new HashMap<>();
        for (Map<String, Object> map : list) {
            String mapKey = String.valueOf(map.get("mapKey"));
            String mapValue = null;
            if (!"".equals(String.valueOf(map.get("mapValue")))) {
                mapValue = String.valueOf(map.get("mapValue"));
            }
            resultMap.put(mapKey, mapValue);
        }
        for (String key : resultMap.keySet()) {
            log.info(key + "-------" + resultMap.get(key));
        }
        return resultMap;
    }

    /**
     * List<Map>转map
     *
     * @param
     * @return
     */
    public Map<String, Object> ListToMapCell(String id, String cellNo) {
        List<Map<String, Object>> list = modifyDetailMapper.getDetailListByReqIdBuild(id, cellNo);
        Map<String, Object> resultMap = new HashMap<>();
        for (Map<String, Object> map : list) {
            String mapKey = String.valueOf(map.get("mapKey"));
            String mapValue = null;
            if (!"".equals(String.valueOf(map.get("mapValue")))) {
                mapValue = String.valueOf(map.get("mapValue"));
            }
            resultMap.put(mapKey, mapValue);
        }
        for (String key : resultMap.keySet()) {
            log.info(key + "-------" + resultMap.get(key));
        }
        return resultMap;
    }

    /**
     * List<Map>转map
     *
     * @param
     * @return
     */
    public Map<String, Object> ListToMap(String id) {
        List<Map<String, Object>> list = modifyDetailMapper.getDetailListByReqId(id);
        Map<String, Object> resultMap = new HashMap<>();
        for (Map<String, Object> map : list) {
            String mapKey = String.valueOf(map.get("mapKey"));
            String mapValue = null;
            if (!"".equals(String.valueOf(map.get("mapValue")))) {
                mapValue = String.valueOf(map.get("mapValue"));
            }
            resultMap.put(mapKey, mapValue);
        }
        for (String key : resultMap.keySet()) {
            log.info(key + "-------" + resultMap.get(key));
        }
        return resultMap;
    }

    /**
     * 获取变更字段合并到目标实体
     *
     * @param id
     * @param targetBean
     * @return
     */
    public T getCompleteEntity(String id, T targetBean) {
        Map<String, Object> map = ListToMap(id);
        return combineEntityUtil.mapCombineEntity(map, targetBean);
    }

    public T getCompleteEntityCell(String id, T targetBean, String buildNo) {
        Map<String, Object> map = ListToMapCell(id, buildNo);
        return combineEntityUtil.mapCombineEntity(map, targetBean);
    }

    /**
     * 获取变更字段合并到目标实体(业主信息专用)
     *
     * @param id
     * @param targetBean
     * @return
     */
    public T getNewCompleteEntity(String id, T targetBean, String reqId) {
        Map<String, Object> map = ListNewToMap(id, reqId);
        return combineEntityUtil.mapCombineEntity(map, targetBean);
    }

    public T getNewCompleteEntityList(String id, T targetBean, String reqId) {
        Map<String, Object> map = ListNewStrutureToMap(id, reqId);
        return combineEntityUtil.mapCombineEntity(map, targetBean);
    }

    /**
     * 保存变更中间表
     *
     * @param oldObject   新对象
     * @param newObject   旧对象
     * @param businessId  业务id
     * @param modifyType  变更类型
     * @param modifyCause 变更理由
     * @param uuid        类型表id
     * @throws IllegalArgumentException
     */
    public int updateToSave(T oldObject, T newObject, String businessId, String modifyType, String modifyCause, String uuid)
            throws IllegalAccessException {
        int num = NumberEnum.ZERO.getNum();
        int count = flowInstanceMapper.getCountByBusinessId(businessId);
        if (count != 0) {
            return num;
        }
        if (0 == count) {
            //若为单元变更
            if (FlowTypeEnum.BASE_CELL_INFO_UPDATE.getType().equals(modifyType)) {
                BaseCellInfo oldCell = (BaseCellInfo) oldObject;
                BaseCellInfo newCell = (BaseCellInfo) newObject;
                num = saveCell(uuid, oldCell, newCell);
                if (num <= 0) {
                    return num;
                }
            } else {
                num = saveModifyDetail(uuid, oldObject, newObject, ShiroUserUtil.getUserName());
                if (num > 0) {
                    num = saveModifyReq(uuid, businessId, modifyType, modifyCause, ShiroUserUtil.getUserName());
                }
            }
        }
        return num;
    }

    /**
     * 保存变更中间表
     *
     * @param oldObject   新对象
     * @param newObject   旧对象
     * @param businessId  业务id
     * @param modifyType  变更类型
     * @param modifyCause 变更理由
     * @param uuid        类型表id
     * @throws IllegalArgumentException
     */
    public int orgUpdate(T oldObject, T newObject, String businessId, String modifyType, String modifyCause, String uuid, String operNo)
            throws IllegalAccessException {
        int num = NumberEnum.ZERO.getNum();
        int count = flowInstanceMapper.getCountByBusinessId(businessId);
        if (count != 0) {
            return num;
        }
        if (0 == count) {
            num = saveModifyDetail(uuid, oldObject, newObject, operNo);
            if (num > 0) {
                num = saveModifyReq(uuid, businessId, modifyType, modifyCause, operNo);
            }
        }
        return num;
    }


    /**
     * 保存ModifyReq
     *
     * @param businessId
     * @param modifyType
     * @param modifyCause
     * @return
     */
    public int saveModifyReq(String uuid, String businessId, String modifyType, String modifyCause, String creater) {
        BaseModifyReq baseModifyReq = new BaseModifyReq();
        baseModifyReq.setModifyType(modifyType);
        baseModifyReq.setModifyCause(modifyCause);
        baseModifyReq.setCreater(creater);
        baseModifyReq.setBusinessId(businessId);
        baseModifyReq.setId(uuid);
        return baseModifyReqMapper.insert(baseModifyReq);
    }

    /**
     * 单元审批流单独
     *
     * @param uuid
     * @param businessId
     * @param modifyType
     * @param modifyCause
     * @return
     */
    public int saveModifyReqCell(String uuid, String businessId, String modifyType, String modifyCause) {
        BaseModifyReq baseModifyReq = new BaseModifyReq();
        baseModifyReq.setModifyType(modifyType);
        baseModifyReq.setModifyCause(modifyCause);
        baseModifyReq.setCreater(ShiroUserUtil.getUserName());
        baseModifyReq.setBusinessId(businessId);
        baseModifyReq.setCreater(ShiroUserUtil.getUserName());
        baseModifyReq.setId(uuid);
        return baseModifyReqMapper.insertId(baseModifyReq);
    }

    /**
     * 保存变更中间表
     *
     * @param oldObject 新对象
     * @param newObject 旧对象
     * @throws IllegalArgumentException
     */
    public int saveModifyDetailCell(BaseHouseInfo oldObject, BaseHouseInfo newObject, String id, String cellNo) throws IllegalAccessException {
        int num = NumberEnum.MINUS_ONE.getNum();
        //查找变更的数据
        List<BaseModifyDetail> list = GetDifferenceUtil.getDifference(oldObject, newObject);
        if (list.size() == 0) {
            return num;
        }
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setCreater(ShiroUserUtil.getUserName());
            list.get(i).setBusinessNextId(id);
            list.get(i).setBusinessId(cellNo);
            list.get(i).setType(NumStrEnum.ONE.getNum());
            list.get(i).setId(UUIDUtil.getUUID());
            num = baseModifyDetailMapper.insert(list.get(i));
            if (num == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "变更失败，请重新操作");
            }
        }
        return num;
    }

    /**
     * 保存变更中间表
     *
     * @param oldObject 新对象
     * @param newObject 旧对象
     * @param uuid      uuid
     * @throws IllegalArgumentException
     */
    public int saveModifyDetail(String uuid, T oldObject, T newObject, String creater) throws IllegalAccessException {
        int num = NumberEnum.MINUS_ONE.getNum();
        //查找变更的数据
        List<BaseModifyDetail> list = GetDifferenceUtil.getDifference(oldObject, newObject);
        if (list.size() == 0) {
            return num;
        }
        for (int i = 0; i < list.size(); i++) {
            BaseModifyDetail baseModifyDetail = list.get(i);
            baseModifyDetail.setId(UUIDUtil.getUUID());
            baseModifyDetail.setCreater(creater);
            num = saveModifyDetail(baseModifyDetail, uuid);
        }
        return num;
    }

    /**
     * 保存变更项
     *
     * @param baseModifyDetail 变更项
     * @param uuid             变更ID
     * @return 变更结果
     */
    public int saveModifyDetail(BaseModifyDetail baseModifyDetail, String uuid) {
        baseModifyDetail.setReqNo(uuid);
        int num = baseModifyDetailMapper.insert(baseModifyDetail);
        if (num == 0) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), "变更失败，请重新操作");
        }
        return num;
    }
/**
 *
 */
    /**
     * @param value
     * @param name
     * @param uuid
     * @param businessId
     * @param type       0新增1删除2修改
     * @return
     */
    public int addBaseModify(String value, String name, String uuid, String businessId, String type, String reqNo, String chinaName) {
        BaseModifyDetail baseModifyDetail = new BaseModifyDetail();
        switch (type) {
            case "0":
                baseModifyDetail.setOldValue("");
                baseModifyDetail.setNewValue(value);
                break;
            case "1":
                baseModifyDetail.setOldValue(value);
                baseModifyDetail.setNewValue("");
                break;
        }
        baseModifyDetail.setColumnName(name);
        baseModifyDetail.setType(NumStrEnum.ONE.getNum());
        baseModifyDetail.setReqNo(reqNo);
        baseModifyDetail.setBusinessId(businessId);
        baseModifyDetail.setBusinessNextId(uuid);
        baseModifyDetail.setName(chinaName);
        baseModifyDetail.setId(UUIDUtil.getUUID());
        int num = baseModifyDetailMapper.insert(baseModifyDetail);
        if (num == 0) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), "添加房屋时添加到变更详情表失败，请重新操作");
        }
        return num;
    }

    /**
     * 房屋删除添加到detail表
     *
     * @param j
     * @param num
     * @param type
     * @return
     */
    private int addModifyNum(String cellName, BaseHouseInfo baseHouseInfo, BaseCellInfo baseCellInfo, int j, int num, String type, String reqNo) {
        String id;
        if (type.equals(NumStrEnum.ZERO.getNum())) {
            id = UUIDUtil.getUUID();
        } else {
            id = baseHouseInfo.getId();
        }
        String room = String.format("%3d", num).replace(" ", NumStrEnum.ZERO.getNum());
        String roomName = j + room.substring(1);
        String subRegionName = "";
        //获取地址
        BaseBuildInfo baseBuildInfo = baseBuildInfoMapper.getInfoById(baseCellInfo.getBuildNo());
        BaseCellInfo b = baseCellInfoMapper.getInfoById(baseCellInfo.getId());
        if (null != baseBuildInfo && null != baseBuildInfo.getSubRegionNo()) {
            BaseSubRegionInfo info = baseSubRegionInfoMapper.getInfo(baseBuildInfo.getSubRegionNo());
            if (null != info && null != info.getSubRegionName()) {
                subRegionName = info.getSubRegionName();
            }
        }
        BaseRegionInfo baseRegionInfo = baseRegionInfoMapper.getInfo(baseBuildInfo.getRegionNo());
        String zoneName = zoneCodeMapper.getZoneName(StringUtils.substring(baseRegionInfo.getZoneCode(), 0, 6));
        String houseSite = zoneName + baseRegionInfo.getRegionName() + subRegionName + baseBuildInfo.getBuildName() + cellName + roomName;
        //旧值，新值，名称，变更名称，房屋还是单元
        if (type.equals(NumStrEnum.ZERO.getNum())) {//若为增加
            addBaseModify(baseCellInfo.getId(), "id", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "id");
            addBaseModify(baseCellInfo.getBuildNo(), "buildNo", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "自然幢id");
            addBaseModify(baseCellInfo.getId(), "cellNo", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "单元id");
            addBaseModify(houseSite, "houseSite", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "房屋地址");
            addBaseModify(j + "", "floorNo", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "层数");
            addBaseModify(room, "roomNo", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "房间号");
            addBaseModify(null, "houseAttr", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "房屋结构");
            addBaseModify(b.getCellType(), "houseUse", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "房屋性质");
            addBaseModify(null, "struArea", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "面积");
            addBaseModify(roomName, "roomName", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "房间名称");
            addBaseModify(NumStrEnum.ZERO.getNum(), "state", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "状态");
            addBaseModify(null, "firstMode", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "初交模式");
            addBaseModify("4", "dueState", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "缴存状态");
            addBaseModify(NumStrEnum.ZERO.getNum(), "flowInProgress", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "在途状态");
            addBaseModify(NumStrEnum.ZERO.getNum(), "ownerState", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "是否有业主");
            addBaseModify(NumStrEnum.ZERO.getNum(), "isAffiliated", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "是否主房");
            addBaseModify(NumStrEnum.ZERO.getNum(), "price", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "价格");
        } else if (type.equals(NumStrEnum.ONE.getNum())) {//若为减少,只保存显示出的数据
            String stru = "";
            if (null != baseHouseInfo.getStruArea()) {
                baseHouseInfo.getStruArea().toString();
            }

            addBaseModify(baseHouseInfo.getId(), "id", id, baseCellInfo.getId(), NumStrEnum.ONE.getNum(), reqNo, "id");
            addBaseModify(baseHouseInfo.getHouseSite(), "houseSite", id, baseCellInfo.getId(), NumStrEnum.ONE.getNum(), reqNo, "房屋地址");
            addBaseModify(baseHouseInfo.getFloorNo(), "floorNo", id, baseCellInfo.getId(), NumStrEnum.ONE.getNum(), reqNo, "层数");
            addBaseModify(baseHouseInfo.getRoomNo(), "roomNo", id, baseCellInfo.getId(), NumStrEnum.ONE.getNum(), reqNo, "房间号");
            addBaseModify(baseHouseInfo.getHouseAttr(), "houseAttr", id, baseCellInfo.getId(), NumStrEnum.ONE.getNum(), reqNo, "房屋结构");
            addBaseModify(baseHouseInfo.getHouseUse(), "houseUse", id, baseCellInfo.getId(), NumStrEnum.ONE.getNum(), reqNo, "房屋性质");
            addBaseModify(stru, "struArea", id, baseCellInfo.getId(), NumStrEnum.ONE.getNum(), reqNo, "面积");
            addBaseModify(baseHouseInfo.getRoomName(), "roomName", id, baseCellInfo.getId(), NumStrEnum.ONE.getNum(), reqNo, "房屋名称");
            addBaseModify(baseHouseInfo.getFirstMode(), "firstMode", id, baseCellInfo.getId(), NumStrEnum.ONE.getNum(), reqNo, "缴存模式");
            addBaseModify(NumStrEnum.ZERO.getNum(), "price", id, baseCellInfo.getId(), NumStrEnum.ZERO.getNum(), reqNo, "价格");
        }
        return 1;
    }

    private void addFloor(int newFool, BaseCellInfo oldObject, String uuid, BaseCellInfo newObject) {
        for (int q = 0; q < Integer.parseInt(newObject.getHouseNum()); q++) {
            int addNo = baseHouseInfoMapper.selectByCellNoAndFloorAndRoom(oldObject.getId(), newFool, q + 1);
            if (addNo > 0) {
                continue;
            }
            int num = addModifyNum(newObject.getCellName(), new BaseHouseInfo(), oldObject, newFool, q + 1, NumStrEnum.ZERO.getNum(), uuid);
            if (num == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "添加房屋失败，请查找原因");
            }
        }
    }

    /**
     * @param newFool   左边初始楼层 右边结束
     * @param oldObject
     * @param uuid
     */
    private void delFloor(String cellName, int newFool, BaseCellInfo oldObject, String uuid) {
        BaseCellInfo baseCellInfo = new BaseCellInfo();
        baseCellInfo.setId(oldObject.getId());
        baseCellInfo.setBuildNo(oldObject.getBuildNo());
        List<BaseHouseInfo> baseHouseInfos = baseHouseInfoMapper.selectByCellNoAndFloor(oldObject.getId(), newFool + "");
        for (BaseHouseInfo b : baseHouseInfos) {
            //查询是否进行房屋变更重
            int num = flowInstanceMapper.getCountByBusinessIdByType(b.getId(), "base_house_info_update");
            if (num > 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), b.getHouseSite() + "房屋正在变更无法删除该房间");
            }
            //查询是否关联副房
            int nu = baseHouseInfoMapper.selectCountByMainHouse(b.getId());
            if (nu > 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), b.getRoomName() + "房屋已经关联副房无法修改");
            }
            //baseHouseInfoMapper.updateStateById(b.getId(), "3");
            num = addModifyNum(cellName, b, baseCellInfo, newFool, Integer.parseInt(b.getRoomNo().substring(2)), NumStrEnum.ONE.getNum(), uuid);
            if (num == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "修改房屋状态失败，请查找原因");
            }
        }

    }

    private Set<Integer> list(int no, int oldStartFloor, Set<Integer> old) {
        for (int i = 0; i < no; i++) {
            int num = oldStartFloor + i;
            if (num == 0) {
                num = 1;
                oldStartFloor++;
            }
            old.add(num);
        }
        return old;
    }

    public int saveCell(String uuid, BaseCellInfo oldObject, BaseCellInfo newObject) throws IllegalAccessException {
        //单元变更，存入表中
        saveModifyDetailCell(uuid, oldObject, newObject);
        String cellName = newObject.getCellName();
        Set<Integer> old = new HashSet<>();
        Set<Integer> oldNew = new HashSet<>();
        Set<Integer> newFloor = new HashSet();
        Set<Integer> resultChange = new HashSet<>();
        int oldStartFloor = Integer.parseInt(oldObject.getStartFloor());
        int newStartFloor = Integer.parseInt(newObject.getStartFloor());
        int no = 0;
        int newnNo = 0;
        if (Integer.parseInt(oldObject.getEndFloor()) > 0 && Integer.parseInt(oldObject.getStartFloor()) > 0 || Integer.parseInt(oldObject.getStartFloor()) < 0 && Integer.parseInt(oldObject.getEndFloor()) < 0) {
            no = Math.abs(Integer.parseInt(oldObject.getEndFloor()) - Integer.parseInt(oldObject.getStartFloor())) + 1;
        } else {
            no = Math.abs(Integer.parseInt(oldObject.getEndFloor()) - Integer.parseInt(oldObject.getStartFloor()));
        }
        if (Integer.parseInt(newObject.getEndFloor()) > 0 && Integer.parseInt(newObject.getStartFloor()) > 0 || Integer.parseInt(newObject.getStartFloor()) < 0 && Integer.parseInt(newObject.getEndFloor()) < 0) {
            newnNo = Math.abs(Integer.parseInt(newObject.getEndFloor()) - Integer.parseInt(newObject.getStartFloor())) + 1;
        } else {
            newnNo = Math.abs(Integer.parseInt(newObject.getEndFloor()) - Integer.parseInt(newObject.getStartFloor()));
        }
        //老数据转set
        list(no, oldStartFloor, old);
        //新数据转set.
     list(newnNo, newStartFloor, newFloor);
        System.out.println(old);
        System.out.println("新修改" + newFloor);
        //差集  resultChange+resultChange
        resultChange.addAll(old);
        resultChange.removeAll(newFloor);
        List<Integer> list = new ArrayList<>();
        Iterator<Integer> iterator = resultChange.iterator();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        resultChange.clear();
        resultChange.addAll(newFloor);
        resultChange.removeAll(old);
        Iterator<Integer> iteratorss = resultChange.iterator();
        while (iteratorss.hasNext()) {
            list.add(iteratorss.next());
        }
        //升序
        Collections.sort(list);
        System.out.println("差集" + list);
        //交集
        resultChange.clear();
        resultChange.addAll(newFloor);
        resultChange.retainAll(old);
        List<Integer> listAll = new ArrayList<>();
        Iterator<Integer> iterators = resultChange.iterator();
        while (iterators.hasNext()) {
            listAll.add(iterators.next());
        }
        Collections.sort(listAll);
        System.out.println("交集" + listAll);
        //户数修改
        //户数遍小
        if (Integer.parseInt(oldObject.getHouseNum()) > Integer.parseInt(newObject.getHouseNum())) {
            int n = Integer.parseInt(oldObject.getHouseNum()) - Integer.parseInt(newObject.getHouseNum());
            for (int j = 0; j < n; j++) {
                BaseCellInfo baseCellInfo = new BaseCellInfo();
                baseCellInfo.setId(oldObject.getId());
                baseCellInfo.setBuildNo(oldObject.getBuildNo());
                String room = String.format("%3d", (Integer.parseInt(newObject.getHouseNum()) + j + 1)).replace(" ", NumStrEnum.ZERO.getNum());
                List<BaseHouseInfo> baseHouseInfos = baseHouseInfoMapper.selectByCellNoAndRoomNo(oldObject.getId(), room);
                for (BaseHouseInfo b : baseHouseInfos) {
                    //查询是否进行房屋变更重
                    int num = flowInstanceMapper.getCountByBusinessIdByType(b.getId(), "base_house_info_update");
                    if (num > 0) {
                        throw new CourseException(ShiroResultEnum.FAIL.getCode(), b.getHouseSite() + "房屋正在变更无法修改");
                    }
                    //查询是否关联副房
                    int nu = baseHouseInfoMapper.selectCountByMainHouse(b.getId());
                    if (nu > 0) {
                        throw new CourseException(ShiroResultEnum.FAIL.getCode(), b.getHouseSite() + "房屋已经关联副房无法修改");
                    }
                    Integer floor = Integer.parseInt(b.getFloorNo());
                    // baseHouseInfoMapper.updateStateById(b.getId(), "3");
                    num = addModifyNum(cellName, b, baseCellInfo, floor, j + 1, NumStrEnum.ONE.getNum(), uuid);
                    if (num == 0) {
                        throw new CourseException(ShiroResultEnum.FAIL.getCode(), "修改房屋失败，请查找原因");
                    }
                }
            }
            //户数变大
        } else if (Integer.parseInt(oldObject.getHouseNum()) < Integer.parseInt(newObject.getHouseNum())) {
            int n = Integer.parseInt(newObject.getHouseNum()) - Integer.parseInt(oldObject.getHouseNum());
            int count = listAll.size();
            for (int nn = 0; nn < count; nn++) {
                for (int j = 0; j < n; j++) {
                    no = Integer.parseInt(String.format("%3d", (Integer.parseInt(oldObject.getHouseNum()) + j + 1)).replace(" ", NumStrEnum.ZERO.getNum()));
                    int addNo = baseHouseInfoMapper.selectByCellNoAndFloorAndRoom(oldObject.getId(), listAll.get(nn), no);
                    if (addNo > 0) {
                        continue;
                    }
                    int num = addModifyNum(cellName, new BaseHouseInfo(), oldObject, listAll.get(nn), Integer.parseInt(oldObject.getHouseNum()) + j + 1, NumStrEnum.ZERO.getNum(), uuid);
                    if (num == 0) {
                        throw new CourseException(ShiroResultEnum.FAIL.getCode(), "添加房屋失败，请查找原因");
                    }
                }
            }
        }
        for (int i = 0; i < list.size(); i++) {
            if (old.contains(list.get(0))){
                delFloor(cellName, list.get(i), oldObject, uuid);
            }else {
                addFloor(list.get(i), oldObject, uuid, newObject);
            }
        }
        return 1;
    }

    /**
     * 单元信息放入表中
     *
     * @param uuid
     * @param oldObject
     * @param newObject
     * @return
     * @throws IllegalAccessException
     */
    public int saveModifyDetailCell(String uuid, BaseCellInfo oldObject, BaseCellInfo newObject) throws IllegalAccessException {
        int num = NumberEnum.MINUS_ONE.getNum();
        //查找变更的数据
        List<BaseModifyDetail> list = GetDifferenceUtil.getDifference(oldObject, newObject);
        if (list == null) {
            return num;
        }
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setCreater(ShiroUserUtil.getUserName());
            list.get(i).setReqNo(uuid);
            list.get(i).setBusinessId(oldObject.getId());
            list.get(i).setType(NumStrEnum.ZERO.getNum());
            list.get(i).setId(UUIDUtil.getUUID());
            num = baseModifyDetailMapper.insert(list.get(i));
            if (num == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "变更失败，请重新操作");
            }
        }
        return num;
    }


    /**
     * 保存房屋调换详情
     *
     * @param beforeChangeBuildMsg 变换前房屋业主信息
     * @param afterChangeBuildMsg  变换后房屋业主信息
     * @param uuid                 变更表ID
     * @return 保存结果
     */
    public int saveModifyHouse(ChangeBuildMsg beforeChangeBuildMsg, ChangeBuildMsg afterChangeBuildMsg, String uuid) {
        BaseBuildModifyDetail baseBuildModifyDetail = new BaseBuildModifyDetail();
        baseBuildModifyDetail.setId(UUIDUtil.getUUID());
        baseBuildModifyDetail.setReqId(uuid);
        baseBuildModifyDetail.setOldAddress(beforeChangeBuildMsg.getHouseSite());
        baseBuildModifyDetail.setOldBalance(beforeChangeBuildMsg.getFundBal());
        baseBuildModifyDetail.setOldBuildState(beforeChangeBuildMsg.getState());
        baseBuildModifyDetail.setOldDepositState(beforeChangeBuildMsg.getDueState());
        baseBuildModifyDetail.setOldName(beforeChangeBuildMsg.getName());
        baseBuildModifyDetail.setOldPayable(beforeChangeBuildMsg.getFirstDue());
        baseBuildModifyDetail.setOldBusinessId(beforeChangeBuildMsg.getId());

        baseBuildModifyDetail.setNewAddress(afterChangeBuildMsg.getHouseSite());
        baseBuildModifyDetail.setNewBalance(afterChangeBuildMsg.getFundBal());
        baseBuildModifyDetail.setNewBuildState(afterChangeBuildMsg.getState());
        baseBuildModifyDetail.setNewDepositState(afterChangeBuildMsg.getDueState());
        baseBuildModifyDetail.setNewName(afterChangeBuildMsg.getName());
        baseBuildModifyDetail.setNewPayable(afterChangeBuildMsg.getFirstDue());
        baseBuildModifyDetail.setNewBusinessId(afterChangeBuildMsg.getId());

        baseBuildModifyDetail.setCreater(ShiroUserUtil.getUserName());
        baseBuildModifyDetail.setModifer(ShiroUserUtil.getUserName());
        // 插入房屋调换表
        return baseBuildModifyDetailMapper.insert(baseBuildModifyDetail);
    }
}